package com.codefish.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codefish.entity.CapitalAccount;
import com.codefish.entity.InMoney;
import com.codefish.entity.InMoneyType;
import com.codefish.mapper.InMoneyDao;
import com.codefish.service.CapitalAccountService;
import com.codefish.service.InMoneyService;
import com.codefish.service.InMoneyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("inMoneyService")
public class InMoneyServiceImpl extends ServiceImpl<InMoneyDao, InMoney> implements InMoneyService {


    @Autowired
    private InMoneyTypeService inMoneyTypeService;

    @Autowired
    private CapitalAccountService capitalAccountService;

    /**
     * 新增单个
     *
     * @param inMoney
     * @return 自增逐渐
     */
    @Override
    public Integer saveOne(InMoney inMoney) {

        boolean save = this.save(inMoney);
        if (save && ObjectUtils.isNotEmpty(inMoney.getId())) {
            return inMoney.getId();
        }

        return null;
    }

    /**
     * 修改单个
     *
     * @param inMoney
     * @return
     */
    @Override
    public boolean updateOne(InMoney inMoney) {

        int updateCount = baseMapper.updateById(inMoney);

        if (updateCount >= 1) {
            return true;
        }

        return false;
    }

    /**
     * 逻辑删除单个
     *
     * @param id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteOne(Integer id) {

        InMoney inMoney = new InMoney();
        inMoney.setId(id);
        inMoney.setIsDel(1);

        boolean flag = this.updateById(inMoney);


        return flag;
    }

    /**
     * 条件分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param familyId
     * @return
     */
    @Override
    public IPage<InMoney> getPageByParams(Integer pageNo, Integer pageSize, Integer familyId) {

        Page<InMoney> page = new Page<>(pageNo, pageSize);

        QueryWrapper<InMoney> qw = new QueryWrapper<>();
        qw.ne("is_del", 1).eq("family_id", familyId);
        qw.orderByDesc("id");

        IPage<InMoney> retPage = baseMapper.selectPage(page, qw);

        this.setNameByCode(retPage.getRecords());

        return retPage;
    }

    /**
     * setNameByCode
     *
     * @param list
     */
    private void setNameByCode(List<InMoney> list) {
        List<Integer> inMonetTypeList = list.stream().filter(one -> ObjectUtils.isNotEmpty(one.getInMoneyType())).map(InMoney::getInMoneyType).collect(Collectors.toList());

        List<Integer> capitalAccountIds = list.stream().filter(one -> ObjectUtils.isNotEmpty(one.getCapitalAccountId())).map(InMoney::getCapitalAccountId).collect(Collectors.toList());


        List<InMoneyType> inMoneyTypes = inMoneyTypeService.selectList(inMonetTypeList);

        List<CapitalAccount> capitalAccounts = capitalAccountService.selectListByIds(capitalAccountIds);


        for (InMoney inMoney : list) {
            if (ObjectUtils.isNotEmpty(inMoney.getInMoneyType())){
                Integer inMoneyType = inMoney.getInMoneyType();
                InMoneyType type = inMoneyTypes.stream().filter(one -> Objects.equals(inMoneyType, one.getId())).findFirst().orElse(null);
                if (!ObjectUtils.isEmpty(type)){
                    inMoney.setInMoneyTypeName(StringUtils.isNotEmpty(type.getTypeName()) ? type.getTypeName() : null);
                }
            }

            if (ObjectUtils.isNotEmpty(inMoney.getCapitalAccountId())){
                Integer capitalAccountId = inMoney.getCapitalAccountId();
                CapitalAccount capitalAccount = capitalAccounts.stream().filter(one -> Objects.equals(capitalAccountId, one.getId())).findFirst().orElse(null);

                if (null != capitalAccount){
                    inMoney.setCapitalAccountName(StringUtils.isEmpty(capitalAccount.getAccountName()) ? null : capitalAccount.getAccountName());
                }
            }
        }
    }
}